//
//  ReaderTopBar.m
//  ebook
//
//  Created by 吴超飞 on 16/1/15.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "ReaderTopBar.h"
#import "UIView+Extension.h"
#import "UIImage+RTTint.h"
#import "ReaderBookModel.h"
#import "BaocuoMenuView.h"
#import "SourceUrlView.h"

#define BarHeight (60 + (iPhoneX?24:0))
#define YSpace (iPhoneX?24:0)

@interface ReaderTopBar()<BaocuoMenuViewDelegate, SourceUrlViewDelegate>{
    
    BOOL isShow,isPopShow;//视图本身是否显示、是否显示pop视图
    BaocuoMenuView *popView;
    UILabel *titleLabel;
    UIButton *changeSourceBtn;
    SourceUrlView *sourceView;
}

@property (nonatomic,retain) ReaderBookModel *readerbookModel;

@end

@implementation ReaderTopBar

-(instancetype)initWithDataController:(ReaderBookModel *)readerBookModel{
    
    self = [super initWithFrame:CGRectMake(0, 0, MTScreenW, BarHeight)];
    self.readerbookModel = readerBookModel;
    
    if (self) {
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MTScreenW, BarHeight)];
        topView.backgroundColor = MTConfigColor(@"MTReaderTopBar_Bg_Color");
        [self addSubview:topView];
        _topView = topView;
        
        sourceView = [[SourceUrlView alloc] initWithFrame:CGRectMake(0, BarHeight, MTScreenW, 25)];
        sourceView.delegate = self;
        
        UIColor *normalTextColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
        UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
        UIButton *backBtn=[[UIButton alloc] initWithFrame:CGRectMake(5, 15+YSpace, 50, 50)];
        _backBtn = backBtn;
        [backBtn setImage:[[UIImage imageNamed:@"fanhui_nl"] rt_tintedImageWithColor:normalTextColor] forState:UIControlStateNormal];
        [backBtn setImage:[[UIImage imageNamed:@"fanhui_sl"] rt_tintedImageWithColor:selectColor] forState:UIControlStateHighlighted];
        [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [backBtn addTarget:self action:@selector(backBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:backBtn];
        
        titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(55, 15+YSpace, MTScreenW/2, 50)];
        titleLabel.textColor = normalTextColor;
        [self addSubview:topView];
        titleLabel.text = _readerbookModel.readerBook.book.bookname;
        titleLabel.font = [UIFont systemFontOfSize:15];
        [topView addSubview:titleLabel];
        
        UIButton *moreBtn=[[UIButton alloc] initWithFrame:CGRectMake(MTScreenW - 50, 15+YSpace, 50, 50)];
        _moreBtn = moreBtn;
        [moreBtn setImage:[[UIImage imageNamed:@"more_nl"] rt_tintedImageWithColor:normalTextColor] forState:UIControlStateNormal];
        [moreBtn setImage:[[UIImage imageNamed:@"more_sl"] rt_tintedImageWithColor:selectColor] forState:UIControlStateSelected];
        [moreBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [moreBtn addTarget:self action:@selector(moreBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:moreBtn];
        changeSourceBtn=[[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(moreBtn.frame)-2-60, 10+YSpace, 60, 60)];
        [changeSourceBtn setTitle:@"换源" forState:UIControlStateNormal];
        [changeSourceBtn setTitleColor:MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color") forState:UIControlStateNormal];
        changeSourceBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [changeSourceBtn addTarget:self action:@selector(changeSourceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:changeSourceBtn];
        
        popView = [[BaocuoMenuView alloc] initWithFrame:CGRectMake(MTScreenW - 115, CGRectGetMaxY(_moreBtn.frame) + 10, 110, 50)];
        popView.delegate = self;
    }
    
    return self;
}

#pragma mark - BaocuoMenuViewDelegate
-(void)baocuoMenuView:(BaocuoMenuView *)view selectContent:(NSInteger)content{
    
    [self moreBtnClick:nil];
    if ([self.delegate respondsToSelector:@selector(readerTopBar:selectContent:)]) {
        [self.delegate readerTopBar:self selectContent:content];
    }
}

-(void)baocuoMenuView:(BaocuoMenuView *)view refreshChapterBtnclick:(UIButton *)btn{
   
    [self moreBtnClick:nil];
    if ([self.delegate respondsToSelector:@selector(readerTopBar:refreshChapterBtnclick:)]) {
        [self.delegate readerTopBar:self refreshChapterBtnclick:btn];
    }
}

-(void)baocuoMenuView:(BaocuoMenuView *)view redirectStoreBtnclick:(UIButton *)btn{
   
    [self moreBtnClick:nil];
    if([self.delegate respondsToSelector:@selector(readerTopBar:redirectStoreBtnclick:)]){
        [self.delegate readerTopBar:self redirectStoreBtnclick:btn];
    }
}

-(void)baocuoMenuView:(BaocuoMenuView *)view clickedTranscodingButtonAtIndex:(NSInteger)buttonIndex{
    
    [self moreBtnClick:nil];
    if ([self.delegate respondsToSelector:@selector(readerTopBar:clickedTranscodingButtonAtIndex:)]) {
        [self.delegate readerTopBar:self clickedTranscodingButtonAtIndex:buttonIndex];
    }
}

-(void)backBtnClick:(id)sender{
    
    if ([self.delegate respondsToSelector:@selector(readerTopBar:backBtnClick:)]) {
        [self.delegate readerTopBar:self backBtnClick:sender];
    }
}

- (void)changeSourceBtnClick:(id)sender {
    
    [popView removeFromSuperview];
    _moreBtn.selected = NO;
    
    if ([self.delegate respondsToSelector:@selector(readerTopBar:changeSourceBtnClick:)]) {
        [self.delegate readerTopBar:self changeSourceBtnClick:sender];
    }
}
-(void)moreBtnClick:(id)sender{
    
    isPopShow = !isPopShow;
    _moreBtn.selected = isPopShow;
    if (isPopShow) {//显示pop视图
        [self hideSourceView];
        popView.y = CGRectGetHeight(self.frame) + 8;
        self.height += CGRectGetHeight(popView.frame) + 8;
        [self addSubview:popView];
        [self bringSubviewToFront:popView];
    }else{
        self.height -= CGRectGetHeight(popView.frame) + 8;
        [popView removeFromSuperview];
        [self showSourceView];
    }
}

-(void)setIsNight:(BOOL)isNight{
    
    _isNight = isNight;
    popView.isNight = isNight;
    sourceView.isNight = isNight;
    
    UIColor *normalTextColor = MTConfigColor(_isNight?@"MTReaderBar_NightNomarlText_Color":@"MTReaderBar_DayNomarlText_Color");
    UIColor *selectColor = MTConfigColor(_isNight?@"MTReaderBar_NightSelected_Color":@"MTReaderBar_DaySelected_Color");
    _topView.backgroundColor = MTConfigColor(isNight?@"MTReaderToolBar_Bg_Color":@"MTReaderToolBar_DayBg_Color");
    titleLabel.textColor = normalTextColor;
    [changeSourceBtn setTitleColor:normalTextColor forState:UIControlStateNormal];
    
    [_moreBtn setImage:[[UIImage imageNamed:@"more_nl"] rt_tintedImageWithColor:normalTextColor] forState:UIControlStateNormal];
    [_moreBtn setImage:[[UIImage imageNamed:@"more_sl"] rt_tintedImageWithColor:selectColor] forState:UIControlStateSelected];
    
    [_backBtn setImage:[[UIImage imageNamed:@"fanhui_nl"] rt_tintedImageWithColor:normalTextColor] forState:UIControlStateNormal];
    [_backBtn setImage:[[UIImage imageNamed:@"fanhui_sl"] rt_tintedImageWithColor:selectColor] forState:UIControlStateHighlighted];
}

-(void)setIsMarked:(BOOL)isMarked{
    
    self.isMarked = isMarked;
}

//加入父视图，并且默认不显示
-(void)addToView:(UIView *)parentView{
    
    CGRect barFrame = self.frame;
    barFrame.origin.y = parentView.frame.origin.y - barFrame.size.height;
    self.frame = barFrame;
    isShow = NO;
    [parentView addSubview:self];
    [parentView bringSubviewToFront:self];
}

-(void)show{
    
    if(!titleLabel.text||titleLabel.text.length==0)//有可能在初始化的时候还没有获取到书籍信息，再次显示视图时需重新取值
        titleLabel.text = _readerbookModel.readerBook.book.bookname;
    
    NSString *sourceUrl = _readerbookModel.readerBook.chapter.sourceurl;
    if (!sourceUrl || sourceUrl.length == 0) {
        sourceView.hidden = YES;
        self.height = BarHeight;
        _topView.height = BarHeight;
    } else {
        sourceView.hidden = NO;
        [self addSubview:sourceView];
        self.height = BarHeight + kSourceUrlViewHeight;
        _topView.height = BarHeight + kSourceUrlViewHeight;
        [sourceView showWithUrlStr:sourceUrl];
    }
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = 0;
        self.frame = origFrame ;
        isShow = YES;
    }];
}

-(void)hide{
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect origFrame = self.frame;
        origFrame.origin.y = -origFrame.size.height;
        self.frame = origFrame ;
    }];
}

-(BOOL)isSHow{
    
    return isShow;
}

-(BOOL)isShowSubView{
    
    return isPopShow;
}

-(void)hideSubView{//收起子视图
    
    [self moreBtnClick:_moreBtn];
}

#pragma mark - sourceUrlView show hide
- (void)hideSourceView {
    
    sourceView.hidden = YES;
    self.height = BarHeight;
    _topView.height = BarHeight;
}

- (void)showSourceView {
    
    NSString *sourceUrl = _readerbookModel.readerBook.chapter.sourceurl;
    if (sourceUrl && sourceUrl.length > 0) {
        sourceView.hidden = NO;
        self.height = 85;
        _topView.height = 85;
    }
}

#pragma mark - sourceUrlDelegate
- (void)sourceUrlView:(SourceUrlView *)view sourceUrlBtnClick:(id)sender {
    
    if ([self.delegate respondsToSelector:@selector(readerTopBar:sourceBtnClick:)]) {
        [self.delegate readerTopBar:self sourceBtnClick:nil];
    }
    
}

@end
